package com.jliang.apps.receiver;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import com.rabbitmq.client.Channel;

/**
 * @author jijinliang
 * @title: RabbitReceiver
 * @projectName spring-boot-rabbitmq-cluster
 * @description: TODO
 * @date 2019/2/27 10:02
 */
@Component("rabbitReceiver")
@Slf4j
public class RabbitReceiver {
    @RabbitListener(
            bindings = @QueueBinding(value = @Queue(value = "queue-1", durable = "true"),
                    exchange = @Exchange(value = "exchange-1", durable = "true", type = "topic", ignoreDeclarationExceptions = "true"), key = "springboot.*")
    )
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws Exception {
        log.info("消费端Payload: " + message.getPayload());
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        log.info("deliveryTag: " + deliveryTag);
        channel.basicAck(deliveryTag, false);
    }

}
